﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ezCloud.Data;

namespace ezCloud.Api.Common
{
    public class InvoiceNumberGenerator
    {
        
        public static int getInvoiceNumber(int hotelId, string invoiceType)
        {
            ezCloudDataContext context = new ezCloudDataContext();
            var invoiceInfo = context.SystemInvoiceNumber.Where(sin => sin.HotelId == hotelId && sin.InvoiceCode == invoiceType).FirstOrDefault();
            if (invoiceInfo == null)
            {
                invoiceInfo = new SystemInvoiceNumber();
                invoiceInfo.HotelId = hotelId;
                invoiceInfo.InvoiceCode = invoiceType;
                invoiceInfo.CurrentInvoiceId = 0;
                context.SystemInvoiceNumber.InsertOnSubmit(invoiceInfo);
                context.SubmitChanges();
            }
            invoiceInfo.CurrentInvoiceId++;
            context.SubmitChanges();
            return invoiceInfo.CurrentInvoiceId;
        }
    }
}